/*     */ package com.zimbra.cs.gal;
/*     */ 
/*     */ import com.zimbra.common.mailbox.ContactConstants;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.AccountConstants;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.SoapProtocol;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.AccountServiceException;
/*     */ import com.zimbra.cs.account.AuthToken;
/*     */ import com.zimbra.cs.account.GalContact;
/*     */ import com.zimbra.cs.account.Group;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.mailbox.Contact;
/*     */ import com.zimbra.cs.service.AuthProvider;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import com.zimbra.soap.type.GalSearchType;
/*     */ import java.util.Arrays;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ import java.util.TreeSet;
/*     */ import org.json.JSONArray;
/*     */ import org.json.JSONException;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class GalGroupMembers
/*     */ {
/*     */   public static abstract class DLMembersResult
/*     */   {
/*     */     protected Set<String> mMembersSet;
/*     */     
/*     */     protected abstract Set<String> getAllMembers()
/*     */       throws ServiceException;
/*     */     
/*     */     protected Set<String> createMembersSet()
/*     */     {
/*  53 */       return new TreeSet(String.CASE_INSENSITIVE_ORDER);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   public static abstract class DLMembers
/*     */     extends GalGroupMembers.DLMembersResult
/*     */   {
/*     */     public abstract int getTotal();
/*     */     
/*     */ 
/*     */     public abstract String getDLZimbraId();
/*     */     
/*     */ 
/*     */     public abstract void encodeMembers(int paramInt1, int paramInt2, Element paramElement);
/*     */     
/*     */ 
/*     */     protected Element encodeMember(Element parent, String member)
/*     */     {
/*  72 */       return parent.addElement("dlm").setText(member);
/*     */     }
/*     */   }
/*     */   
/*     */   public static class ContactDLMembers extends GalGroupMembers.DLMembers {
/*     */     private Contact mContact;
/*     */     private JSONArray mMembers;
/*     */     
/*     */     public ContactDLMembers(Contact contact) {
/*  81 */       this.mContact = contact;
/*     */       
/*  83 */       String members = this.mContact.get(ContactConstants.A_member);
/*     */       
/*  85 */       if (members != null) {
/*     */         try {
/*  87 */           this.mMembers = Contact.getMultiValueAttrArray(members);
/*     */         } catch (JSONException e) {
/*  89 */           ZimbraLog.account.warn("unable to get members from Contact " + this.mContact.getId(), e);
/*     */         }
/*     */       }
/*     */     }
/*     */     
/*     */     public int getTotal()
/*     */     {
/*  96 */       if (this.mMembers == null) {
/*  97 */         return 0;
/*     */       }
/*  99 */       return this.mMembers.length();
/*     */     }
/*     */     
/*     */     public String getDLZimbraId()
/*     */     {
/* 104 */       return this.mContact.get(ContactConstants.A_zimbraId);
/*     */     }
/*     */     
/*     */     public void encodeMembers(int beginIndex, int endIndex, Element resp)
/*     */     {
/* 109 */       if (this.mMembers == null) {
/* 110 */         return;
/*     */       }
/* 112 */       if (endIndex <= getTotal()) {
/*     */         try {
/* 114 */           for (int i = beginIndex; i < endIndex; i++) {
/* 115 */             encodeMember(resp, this.mMembers.getString(i));
/*     */           }
/*     */         } catch (JSONException e) {
/* 118 */           ZimbraLog.account.warn("unable to get members from Contact " + this.mContact.getId(), e);
/*     */         }
/*     */       }
/*     */     }
/*     */     
/*     */     public Set<String> getAllMembers()
/*     */     {
/* 125 */       if (this.mMembersSet != null) {
/* 126 */         return this.mMembersSet;
/*     */       }
/* 128 */       this.mMembersSet = createMembersSet();
/*     */       
/*     */ 
/* 131 */       if (this.mMembers != null) {
/*     */         try {
/* 133 */           for (int i = 0; i < getTotal(); i++) {
/* 134 */             this.mMembersSet.add(this.mMembers.getString(i));
/*     */           }
/*     */         } catch (JSONException e) {
/* 137 */           ZimbraLog.account.warn("unable to get members from Contact " + this.mContact.getId(), e);
/*     */         }
/*     */       }
/*     */       
/* 141 */       return this.mMembersSet;
/*     */     }
/*     */   }
/*     */   
/*     */   public static class GalContactDLMembers extends GalGroupMembers.DLMembers
/*     */   {
/*     */     private GalContact mGalContact;
/*     */     private String[] mMembers;
/*     */     private Set<String> mMembersSet;
/*     */     
/*     */     private GalContactDLMembers(GalContact galContact) {
/* 152 */       this.mGalContact = galContact;
/*     */       
/* 154 */       Object members = this.mGalContact.getAttrs().get(ContactConstants.A_member);
/* 155 */       if ((members instanceof String)) {
/* 156 */         this.mMembers = new String[] { (String)members };
/* 157 */       } else if ((members instanceof String[])) {
/* 158 */         this.mMembers = ((String[])members);
/*     */       }
/*     */     }
/*     */     
/*     */     public int getTotal() {
/* 163 */       if (this.mMembers == null) {
/* 164 */         return 0;
/*     */       }
/* 166 */       return this.mMembers.length;
/*     */     }
/*     */     
/*     */     public String getDLZimbraId()
/*     */     {
/* 171 */       return this.mGalContact.getSingleAttr(ContactConstants.A_zimbraId);
/*     */     }
/*     */     
/*     */     public void encodeMembers(int beginIndex, int endIndex, Element resp)
/*     */     {
/* 176 */       if (this.mMembers == null) {
/* 177 */         return;
/*     */       }
/* 179 */       if (endIndex <= getTotal()) {
/* 180 */         for (int i = beginIndex; i < endIndex; i++) {
/* 181 */           encodeMember(resp, this.mMembers[i]);
/*     */         }
/*     */       }
/*     */     }
/*     */     
/*     */     public Set<String> getAllMembers()
/*     */     {
/* 188 */       if (this.mMembersSet != null) {
/* 189 */         return this.mMembersSet;
/*     */       }
/* 191 */       this.mMembersSet = createMembersSet();
/*     */       
/*     */ 
/* 194 */       if (this.mMembers != null) {
/* 195 */         this.mMembersSet.addAll(Arrays.asList(this.mMembers));
/*     */       }
/*     */       
/* 198 */       return this.mMembersSet;
/*     */     }
/*     */   }
/*     */   
/*     */   public static class LdapDLMembers extends GalGroupMembers.DLMembers
/*     */   {
/*     */     private Group group;
/*     */     private String[] allMembers;
/*     */     
/*     */     public LdapDLMembers(Group group) throws ServiceException {
/* 208 */       this.group = group;
/* 209 */       this.allMembers = Provisioning.getInstance().getGroupMembers(group);
/*     */     }
/*     */     
/*     */     public void encodeMembers(int beginIndex, int endIndex, Element resp)
/*     */     {
/* 214 */       if (endIndex <= getTotal()) {
/* 215 */         for (int i = beginIndex; i < endIndex; i++) {
/* 216 */           encodeMember(resp, this.allMembers[i]);
/*     */         }
/*     */       }
/*     */     }
/*     */     
/*     */     public String getDLZimbraId()
/*     */     {
/* 223 */       return this.group.getId();
/*     */     }
/*     */     
/*     */     public int getTotal()
/*     */     {
/* 228 */       return this.allMembers.length;
/*     */     }
/*     */     
/*     */     protected Set<String> getAllMembers() throws ServiceException
/*     */     {
/* 233 */       return this.group.getAllMembersSet();
/*     */     }
/*     */   }
/*     */   
/*     */   public static class ProxiedDLMembers extends GalGroupMembers.DLMembersResult
/*     */   {
/*     */     private Element mResponse;
/*     */     Set<String> mMembersSet;
/*     */     
/*     */     public ProxiedDLMembers(Element response) {
/* 243 */       this.mResponse = response;
/* 244 */       this.mResponse.detach();
/*     */     }
/*     */     
/*     */     public Element getResponse() {
/* 248 */       return this.mResponse;
/*     */     }
/*     */     
/*     */     public Set<String> getAllMembers()
/*     */     {
/* 253 */       if (this.mMembersSet != null) {
/* 254 */         return this.mMembersSet;
/*     */       }
/* 256 */       this.mMembersSet = createMembersSet();
/*     */       
/*     */ 
/* 259 */       for (Element eDLM : this.mResponse.listElements("dlm")) {
/* 260 */         this.mMembersSet.add(eDLM.getText());
/*     */       }
/*     */       
/* 263 */       return this.mMembersSet;
/*     */     }
/*     */   }
/*     */   
/*     */   private static class GalGroupMembersCallback extends GalSearchResultCallback {
/*     */     private GalGroupMembers.DLMembersResult mDLMembers;
/*     */     
/*     */     GalGroupMembersCallback(GalSearchParams params) {
/* 271 */       super();
/*     */     }
/*     */     
/*     */     public boolean passThruProxiedGalAcctResponse()
/*     */     {
/* 276 */       return true;
/*     */     }
/*     */     
/*     */     GalGroupMembers.DLMembersResult getDLMembers() {
/* 280 */       return this.mDLMembers;
/*     */     }
/*     */     
/*     */     public void handleProxiedResponse(Element resp)
/*     */     {
/* 285 */       this.mDLMembers = new GalGroupMembers.ProxiedDLMembers(resp);
/*     */     }
/*     */     
/*     */     public Element handleContact(Contact contact) throws ServiceException
/*     */     {
/* 290 */       this.mDLMembers = new GalGroupMembers.ContactDLMembers(contact);
/* 291 */       return null;
/*     */     }
/*     */     
/*     */     public void handleContact(GalContact galContact) throws ServiceException
/*     */     {
/* 296 */       this.mDLMembers = new GalGroupMembers.GalContactDLMembers(galContact, null);
/*     */     }
/*     */     
/*     */ 
/*     */     public void handleElement(Element e)
/*     */       throws ServiceException
/*     */     {}
/*     */   }
/*     */   
/*     */   public static DLMembersResult searchGal(ZimbraSoapContext zsc, Account account, String groupName, Element request)
/*     */     throws ServiceException
/*     */   {
/* 308 */     GalSearchParams params = new GalSearchParams(account, zsc);
/* 309 */     params.setQuery(groupName);
/* 310 */     params.setType(GalSearchType.group);
/* 311 */     params.setLimit(1);
/* 312 */     params.setFetchGroupMembers(true);
/* 313 */     params.setRequest(request);
/* 314 */     GalGroupMembersCallback callback = new GalGroupMembersCallback(params);
/* 315 */     params.setResultCallback(callback);
/*     */     
/* 317 */     GalSearchControl gal = new GalSearchControl(params);
/* 318 */     gal.search();
/* 319 */     return callback.getDLMembers();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static Set<String> getGroupMembers(String groupName, Account account)
/*     */     throws ServiceException
/*     */   {
/* 335 */     AuthToken adminAuthToken = AuthProvider.getAdminAuthToken();
/* 336 */     ZimbraSoapContext zsc = new ZimbraSoapContext(adminAuthToken, account.getId(), SoapProtocol.Soap12, SoapProtocol.Soap12);
/*     */     
/* 338 */     Element request = Element.create(SoapProtocol.Soap12, AccountConstants.GET_DISTRIBUTION_LIST_MEMBERS_REQUEST);
/* 339 */     Element eDL = request.addElement("dl").setText(groupName);
/*     */     
/* 341 */     DLMembersResult dlMembersResult = searchGal(zsc, account, groupName, request);
/*     */     
/* 343 */     if (dlMembersResult == null) {
/* 344 */       throw AccountServiceException.NO_SUCH_DISTRIBUTION_LIST(groupName);
/*     */     }
/*     */     
/* 347 */     return dlMembersResult.getAllMembers();
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/gal/GalGroupMembers.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */